package psrc.bd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import psrc.logic.util.beans.ClienteBean;

public class ClienteDAO extends AbstractDAO {

	public ClienteDAO(Connection conn) {
		super(conn);
	}

	@Override
	public <T> void adiciona(T _bean)throws SQLException {
		ClienteBean bean = (ClienteBean) _bean;
		String sql = "insert into Cliente "
				+ "(razao_social, nome_fantasia, logradouro, numero, complemento, bairro, cidade, estado, cep, cnpj, inscricao_estadual, telefone_1, telefone_2, email, comprador) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setString(1, bean.getRazaoSocial());
			stmt.setString(2, bean.getNomeFantasia());
			stmt.setString(3, bean.getLogradouro());
			stmt.setString(4, bean.getNumero());
			stmt.setString(5, bean.getComplemento());
			stmt.setString(6, bean.getBairro());
			stmt.setString(7, bean.getCidade());
			stmt.setString(8, bean.getEstado());
			stmt.setString(9, bean.getCep());
			stmt.setString(10, bean.getCnpj());
			stmt.setString(11, bean.getInscricaoEstadual());
			stmt.setString(12, bean.getTelefone1());
			stmt.setString(13, bean.getTelefone2());
			stmt.setString(14, bean.getEmail());
			stmt.setString(15, bean.getComprador());
			//stmt.setInt(16, bean.getFkRepresentante());
			//stmt.setInt(17, bean.getFkRede());
			
			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> void remove(T _bean)  throws SQLException{
		ClienteBean bean = (ClienteBean) _bean;
		try {
			PreparedStatement stmt = connection.prepareStatement("delete from Cliente where id_cliente=?");

			stmt.setInt(1, Integer.parseInt(bean.getIdCliente()));

			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> void edita(T _bean) throws SQLException{
		ClienteBean bean = (ClienteBean) _bean;

		String sql = "update Cliente set " +"razao_social=?, nome_fantasia=?, logradouro=?, numero=?, complemento=?, bairro=?, cidade=?, estado=?, cep=?, cnpj=?, inscricao_estadual=?, telefone_1=?, telefone_2=?, email=?, comprador=?, fk_representante=?, fk_rede=? where cliente=?";
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setString(1, bean.getRazaoSocial());
			stmt.setString(2, bean.getNomeFantasia());
			stmt.setString(3, bean.getLogradouro());
			stmt.setString(4, bean.getNumero());
			stmt.setString(5, bean.getComplemento());
			stmt.setString(6, bean.getBairro());
			stmt.setString(7, bean.getCidade());
			stmt.setString(8, bean.getEstado());
			stmt.setString(9, bean.getCep());
			stmt.setString(10, bean.getCnpj());
			stmt.setString(11, bean.getInscricaoEstadual());
			stmt.setString(12, bean.getTelefone1());
			stmt.setString(13, bean.getTelefone2());
			stmt.setString(14, bean.getEmail());
			stmt.setString(15, bean.getComprador());
			stmt.setInt(16, bean.getFkRepresentante());
			stmt.setInt(17, bean.getFkRede());
			stmt.setInt(18, Integer.parseInt(bean.getIdCliente()));
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public <T> List<T> seleciona() throws SQLException{
		List<ClienteBean> clientes = new ArrayList<ClienteBean>();

		try {
			PreparedStatement stmt = connection.prepareStatement("select * from Cliente");

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				ClienteBean bean = new ClienteBean();
				bean.setRazaoSocial(rs.getString("razao_social"));
				bean.setNomeFantasia(rs.getString("nome_fantasia"));
				bean.setLogradouro(rs.getString("logradouro"));
				bean.setNumero(rs.getString("numero"));
				bean.setComplemento(rs.getString("complemento"));
				bean.setBairro(rs.getString("bairro"));
				bean.setCidade(rs.getString("cidade"));
				bean.setEstado(rs.getString("estado"));
				bean.setCep(rs.getString("cep"));
				bean.setCnpj(rs.getString("cnpj"));
				bean.setInscricaoEstadual(rs.getString("inscricao_estadual"));
				bean.setTelefone1(rs.getString("telefone_1"));
				bean.setTelefone2(rs.getString("telefone_2"));
				bean.setEmail(rs.getString("email"));
				bean.setComprador(rs.getString("comprador"));
				bean.setFkRepresentante(rs.getInt("fk_representante"));
				bean.setFkRede(rs.getInt("fk_rede"));
				bean.setIdCliente(rs.getString("id_cliente"));

				clientes.add(bean);
			}
			rs.close();
			stmt.close();
			return (List<T>) clientes;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

@Override
	public <T> List<T> selecionaPorId(T param) throws SQLException {
		List<ClienteBean> clientes = new ArrayList<ClienteBean>();
		ClienteBean user = (ClienteBean) param;
		try {
			PreparedStatement stmt = connection
					.prepareStatement("select * from Cliente where id_cliente=?");
			stmt.setString(1, user.getIdCliente());
			ResultSet rs = stmt.executeQuery();
			
			while (rs.next()) {
				ClienteBean bean = new ClienteBean();
				bean.setRazaoSocial(rs.getString("razao_social"));
				bean.setNomeFantasia(rs.getString("nome_fantasia"));
				bean.setLogradouro(rs.getString("logradouro"));
				bean.setNumero(rs.getString("numero"));
				bean.setComplemento(rs.getString("complemento"));
				bean.setBairro(rs.getString("bairro"));
				bean.setCidade(rs.getString("cidade"));
				bean.setEstado(rs.getString("estado"));
				bean.setCep(rs.getString("cep"));
				bean.setCnpj(rs.getString("cnpj"));
				bean.setInscricaoEstadual(rs.getString("inscricao_estadual"));
				bean.setTelefone1(rs.getString("telefone_1"));
				bean.setTelefone2(rs.getString("telefone_2"));
				bean.setEmail(rs.getString("email"));
				bean.setComprador(rs.getString("comprador"));
				bean.setFkRepresentante(rs.getInt("fk_representante"));
				bean.setFkRede(rs.getInt("fk_rede"));
				bean.setIdCliente(rs.getString("id_cliente"));

				clientes.add(bean);
			}
			rs.close();
			stmt.close();
			return (List<T>) clientes;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}
}
